
package com.rasenhund.android.paintstock.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rasenhund.android.paintstock.schema.Paint;
import com.rasenhund.android.paintstock.schema.Series;

/**
 * シリーズ情報 Data Access Object
 */
public class SeriesDao extends BaseDao {

    public SeriesDao(Context context) {
        super(context);
    }

    /**
     * シリーズ一覧の取得
     * 
     * @return
     */
    public ArrayList<Series> selectAll() {

        ArrayList<Series> seriesList = new ArrayList<Series>();
        
        // @formatter:off
        String sSql = "";
        sSql += "select _id";
        sSql += "     , name";
        sSql += "  from Series";
        sSql += " order by sort";
        // @formatter:on

        SQLiteDatabase db = SQLiteDatabase.openDatabase(getDbPath(), null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);

        try {

            Cursor cursor = db.rawQuery(sSql, null);
            cursor.moveToFirst();

            if (cursor.getCount() > 0) {

                for (int i = 0; i < cursor.getCount(); i++) {

                    Series series = new Series();

                    series.set_id(cursor.getInt(0));
                    series.setName(cursor.getString(1));

                    seriesList.add(series);

                    cursor.moveToNext();

                }

            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            db.close();
        }

        return seriesList;

    }

    /**
     * シリーズ内塗料一覧の取得
     * 
     * @param seriesId
     * @return
     */
    public ArrayList<Paint> selectAll(int seriesId) {

        ArrayList<Paint> paintList = new ArrayList<Paint>();

        // @formatter:off
        String sSql = "";
        sSql += "select _id";
        sSql += "     , name";
        sSql += "     , seriesId";
        sSql += "  from Paint";
        sSql += " where seriesId = " + String.valueOf(seriesId);
        sSql += " order by seriesId";
        sSql += "     , sort";
        // @formatter:on

        SQLiteDatabase db = SQLiteDatabase.openDatabase(getDbPath(), null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);

        try {

            Cursor cursor = db.rawQuery(sSql, null);
            cursor.moveToFirst();

            if (cursor.getCount() > 0) {

                for (int i = 0; i < cursor.getCount(); i++) {

                    Paint paint = new Paint();

                    paint.set_id(cursor.getInt(0));
                    paint.setName(cursor.getString(1));
                    paint.setSeriesId(cursor.getInt(2));

                    paintList.add(paint);

                    cursor.moveToNext();

                }

            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            db.close();
        }

        return paintList;
    }

}
